package com.xs.designpattern;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * 星星纪念品
 *
 * @author xs
 */
@Slf4j
@AllArgsConstructor
public class Star {
    private StarType type;
    private int ageYears;
    private int massTons;

    /**
     * 时间流逝
     */
    public void timePasses() {
        ageYears *= 2;
        massTons *= 8;
        switch (type) {
            case RED_GIANT:
                type = StarType.WHITE_DWARF;
                break;
            case SUN:
                type = StarType.RED_GIANT;
                break;
            case SUPERNOVA:
                type = StarType.DEAD;
                break;
            case WHITE_DWARF:
                type = StarType.SUPERNOVA;
                break;
            case DEAD: {
                ageYears *= 2;
                massTons = 0;
            }
            break;
            default:
                log.info("没有该类型的星星！");
        }
    }

    /**
     * 获取纪念品
     *
     * @return StarMementoInternal
     */
    StarMemento getMemento() {
        StarMementoInternal state = new StarMementoInternal();
        state.setAgeYears(ageYears);
        state.setMassTons(massTons);
        state.setType(type);
        return state;
    }

    /**
     * 设置纪念品
     *
     * @param memento StarMemento
     */
    void setMemento(StarMemento memento) {
        StarMementoInternal state = (StarMementoInternal)memento;
        this.type = state.getType();
        this.ageYears = state.getAgeYears();
        this.massTons = state.getMassTons();
    }

    @Override
    public String toString() {
        return String.format("%s 年龄: %d years 重量: %d 吨", type.toString(), ageYears, massTons);
    }

    /**
     * StarMemento 实现类
     */
    @Getter
    @Setter
    private static class StarMementoInternal implements StarMemento {

        private StarType type;
        private int ageYears;
        private int massTons;
    }
}
